package sword.T48;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 18362
 * @Create: 2022-04-09 18:26:22 星期六
 */

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int left = 0, right = 0, maxLen = 0;
        boolean flag = true;
        Map<Character, Integer> window = new HashMap<>();
        while (right < s.length()) {
            char c = s.charAt(right++);
            int originVal = window.getOrDefault(c, 0);
            window.put(c, originVal+1);
            if (originVal == 1)
                flag = false;
            else
                maxLen = Math.max(maxLen, right-left);
            while (!flag) {
                char d = s.charAt(left++);
                int t = window.get(d);
                if (t == 1) {
                    window.remove(d);
                } else {
                    flag = true;
                    window.put(d, 1);
                }
            }
        }
        return maxLen;
    }
}